package com.example.webdome.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.webdome.mapper.LabelMapper;
import com.example.webdome.pojo.Label;
import com.example.webdome.pojo.Result;
import com.example.webdome.service.LabelService;
import com.example.webdome.util.JwtUtil;
import com.example.webdome.util.decryptToken;
import com.example.webdome.util.spiltUtils;
import com.example.webdome.util.userIdUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

@Service
@Slf4j
public class LabelServiceImpl extends ServiceImpl<LabelMapper, Label> implements LabelService {
    @Resource
    private spiltUtils spiltUtils;
    @Resource
    private userIdUtils userIdUtils;
    @Resource
    private LabelMapper labelMapper;
    @Resource
    private decryptToken decryptToken;

    @Override
    public Result saveLabel(Label label, String token) {
        String pe = decryptToken.decrypt(token);
        if (pe != null) {
            String email = spiltUtils.getEmail(pe);
            int userId = userIdUtils.getUserId(email);
            label.setUserId(userId);
            boolean save = save(label);
            if (save) {
                return Result.success("添加成功！");
            }
            return Result.failed("添加失败");
        }
        return Result.failed("您还没有登录！");
    }

    @Override
    public Result getLabelByUserName(String token) {
        String pe = decryptToken.decrypt(token);
        if (pe != null) {
            String email = spiltUtils.getEmail(pe);
            Integer userId = userIdUtils.getUserId(email);
            QueryWrapper<Label> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("userId", userId);
            Label label = labelMapper.selectOne(queryWrapper);
            return Result.success(label);
        }
        return Result.failed("您还没有登录！");
    }
}
